<template>
  <div class="deepOriginal" v-loading="loading1" element-loading-text="正在发布中，请耐心等待...">
    <div class="body">
      <div class="head">
        <transition name="fade" mode="out-in">
          <el-form :model="ruleForm" style="width: 100%;" v-if="inputState" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="题材名称" prop="source_text">
              <el-input v-model="ruleForm.source_text" placeholder="关键词（必填）" clearable @focus="inputevent(true)"
              @blur="inputevent(false)"></el-input>
            </el-form-item>
            <el-form-item label="字数要求" prop="words">
              <el-input v-model="ruleForm.words" placeholder="输入想要生成的文章字数" clearable></el-input>
            </el-form-item>
            <el-form-item label="附加要求">
              <el-input v-model="ruleForm.source_ask" placeholder="需要突出的观点/内容(选填)" clearable></el-input>
            </el-form-item>
          </el-form>
        </transition>
        <div class="form-btn">
          <el-button @click="record">7天记录</el-button>
          <div>
            <el-button type="primary" :disabled="loading" @click="synonym('ruleForm')">一键生成</el-button>
            <el-button @click="reset('ruleForm')">重置</el-button>
          </div>
        </div>
      </div>
      <div class="content">
        <!-- <div class="title">
          <div><el-input v-model="title" placeholder="请输入文章标题"></el-input></div>
          <el-button type="primary" @click="generate" style="margin-left: 1%;" :disabled="editorData == ''">智能标题</el-button>
        </div> -->
        <Tinymce v-model="editorData"></Tinymce>
        <div class="submitArticle" v-if="editorData&&value1">
          <span>发布到</span>
          <el-select v-model="form.type" style="width: 25%;" placeholder="发布地址" @change="changeType">
            <el-option v-for="item in typeOption" :key="item.site_id" :label="item.name" :value="item.type">
            </el-option>
          </el-select>
          <el-select v-model="form.module" style="width: 25%;" placeholder="发布方式">
            <el-option v-if="form.type == 10" label="动态" value="10"></el-option>
            <el-option label="图文" value="20"></el-option>
          </el-select>
          <el-button type="primary" :disabled="loading" @click="submit">确定</el-button>
          <el-button type="primary" :disabled="loading" @click="saveArticle" :loading="sa_loading">保存</el-button>
        </div>
      </div>
    </div>
    <div class="types">
      <div class="title">题材类型</div>
      <div class="btn-content">
        <button
          v-for="(btn, index) in options"
          :key="index"
          @click="btnChange(btn,index)"
          :class="{ active: currentBtn === index }"
        >
          {{ btn.label }}
        </button>
      </div>
      <el-input v-if="options[currentBtn].value == 80" v-model="ruleForm.theme_text" placeholder="输入想要生成的题材类型" clearable></el-input>
    </div>
    <el-dialog
      title="测试记录"
      :visible.sync="dialogVisible"
      width="50%">
      <el-table
        :data="tableData"
        @row-click="onRowClick"
        style="width: 100%">
        <el-table-column
          prop="cache_article_id"
          align="center"
          width="70"
          label="任务ID">
        </el-table-column>
        <el-table-column
          prop="create_time"
          align="center"
          label="提交时间">
        </el-table-column>
        <el-table-column
          prop="source_text"
          align="center"
          show-overflow-tooltip
          label="标题">
        </el-table-column>
        <el-table-column
          prop="status"
          align="center"
          label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 10">生成中</span>
            <span v-if="scope.row.status == 20">生成完成</span>
            <span v-if="scope.row.status == 30">生成失败</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          align="center"
          label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.is_release == 0">未发布</span>
            <span v-else>已发布</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" v-if="scope.row.is_release == 0" :disabled="scope.row.status != 20" @click.stop="submitDia(scope.row)">发布</el-button>
            <el-button size="mini" @click.stop="delArticle(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="page"
          :page-size="per_page"
          layout="total, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
      <el-dialog
        width="30%"
        title="发布内容"
        :visible.sync="innerVisible"
        append-to-body>
        <span>发布到</span>
        <el-select v-model="form.type" @change="changeType" style="width: 25%;" placeholder="发布地址">
          <el-option v-for="item in typeOption" :key="item.site_id" :label="item.name" :value="item.type">
          </el-option>
        </el-select>
        <el-select v-model="form.module" style="width: 25%;" placeholder="发布方式">
          <el-option v-if="form.type == 10" label="动态" value="10"></el-option>
          <el-option label="图文" value="20"></el-option>
          <!-- <el-option v-for="item in moduleOption" :key="item.value" :label="item.name" :value="item.value">
          </el-option> -->
        </el-select>
        <el-button type="primary" :disabled="loading" @click="submit">确定</el-button>
      </el-dialog>
    </el-dialog>
    <el-drawer
      title="智能生成标题"
      :visible.sync="drawer"
      :direction="direction">
      <!-- <span>我来啦!</span> -->
    </el-drawer>
    <!-- <div class="desc">
      <div>说明：</div>
      <div>1. 每篇文章都由深度原创机器人根据您提供的关键词撰写，100%原创，即使关键词相同，每次生成的内容也不一样。</div>
      <div>2. 版本区别：迷你版约300-500字，专业版约500-800字，旗舰版约700-1500字，DIY版主要使用附加要求规定生成效果。扣费单价相同。</div>
      <div>3. 文章生成可能耗时几十秒甚至数分钟，请您耐心等待。有一定概率生成失败，失败不扣积分。</div>
      <div>4. 生成的内容，系统存储时间为7天，之后将自动删除，生成之后请尽快自行存储。</div>
      <div>5. 所有内容完全由机器生成，不代表我们赞同文中的观点。如果您愿意使用机器人撰写的文章，请自行人工审核，并承担所有风险。</div>
      <div>6. 如需自动从词库取词生成文章，并自动发布到对应站点，请到 功能细节设置 页面自行启用深度原创。</div>
    </div> -->
  </div>
</template>

<script>
import { rewriteDepth,release,cache_article,delArticle,edit_article } from "@/api/aiChange";
import { list } from '@/api/example'
import Tinymce from "@/components/tinymce";
import { EventBus } from '@/utils/eventBus';
export default {
  name: 'deepOriginal',
  components: {
    Tinymce
  },
  props: {
    inputState: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      tabs: [
        { title: '深度改写', link: '/aichange/deepRewrite' },
        { title: '深度原创', link: '/aichange/aiChange/deepOriginal' },
        { title: '原创标题', link: '/aichange/aiChange/deepOriginalTitle' },
        // ...更多tab
      ],
      iconText: 'el-icon-arrow-up',
      currentBtn: 0,
      inputState: true,
      title: '',
      input: '',
      editorData: '',
      value1: '',
      siteId: '',
      drawer: false,
      direction: 'rtl',
      dialogVisible: false,
      innerVisible: false,
      page: 1,
      per_page: 15,
      total: 0,
      loading: false,
      loading1: false,
      tableData: [],
      form: {
        module: '',
        type: ''
      },
      ruleForm: {
        words: 200,
        theme: 10,
      },
      rules: {
        words: [
          { required: true, message: '请输入想要生成文章的字数', trigger: 'blur' }
        ],
        source_text: [
          { required: true, message: '请输入想要生成的文章主题', trigger: 'blur' }
        ],
        theme: [
          { required: true, message: '请选择题材类型', trigger: 'change' }
        ],
      },
      options: [
        {
          label: '营销文章',
          value: 10
        },{
          label: '新闻报道',
          value: 20
        },{
          label: '分析报告',
          value: 30
        },{
          label: '广告文案',
          value: 40
        },{
          label: '记叙文',
          value: 50
        },{
          label: '抒情散文',
          value: 60
        },{
          label: '旅游攻略',
          value: 70
        },{
          label: '其他',
          value: 80
        },
      ],
      typeOption: [
        {
          name: '百家号',
          value: 10
        }, {
          name: '头条号',
          value: 20
        }, {
          name: '微信公众号',
          value: 30
        }, {
          name: '企鹅号',
          value: 40
        }, {
          name: '大鱼号',
          value: 50
        },
      ],
      moduleOption: [],
      images: [],
      title: '',
      sa_loading: false
    }
  },
  computed: {

  },
  mounted() {
    this.getTypeList()
  },
  methods: {
    changeInput() {
      this.inputState = !this.inputState
      if (this.inputState) {
        this.iconText = 'el-icon-arrow-down'
      } else {
        this.iconText = 'el-icon-arrow-up'
      }
    },
    btnChange(item,index) {
      this.currentBtn = index
      this.ruleForm.theme = this.options[index].value
      // this.$router.push(item.link)
    },
    inputevent(e) {
      console.log(e)
      EventBus.$emit('siblingEvent', e);
    },
    synonym(formName) {
      this.editorData = ''
      this.$refs[formName].validate((valid) => {
          if (valid) {
            const query = {}
            query.form = this.ruleForm
            query.form.type = 10
            this.loading = true
            rewriteDepth(query).then(res => {
              this.$message.success(res.message)
              this.ruleForm.source_text = ''
              this.ruleForm.source_ask = ''
              this.ruleForm.theme = ''
              // this.editorData = res.data.info.htmlContent
              this.loading = false
            }).catch(() => {
              this.ruleForm.source_text = ''
              this.ruleForm.source_ask = ''
              this.ruleForm.theme = ''
              this.loading = false
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

    },
    reset(formName) {
      this.ruleForm = {
        words: 200,
        theme: 10,
      },
      this.currentBtn = 0
      // this.$refs[formName].resetFields();
      this.editorData = ''
    },
    generate() {
      this.drawer = true
    },
    getTypeList() {
      list().then(res=> {
        console.log(res)
        this.typeOption = res.data.list.data
      })
    },
    changeType(val) {
      console.log(val)
      this.form.module = ''
      this.typeOption.map(item => {
        if (val == item.type) {
          this.siteId = item.site_id
        }
      })
    },
    delArticle(row) {
      this.$confirm('是否删除该条记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delArticle({cacheArticleId: row.cache_article_id}).then(res=> {
          this.$message.success(res.message)
          this.record()
        })
      })
    },
    handleSizeChange(val) {
      this.per_page = val
      this.record()
    },
    handleCurrentChange(val) {
      this.page = val
      this.record()
    },
    saveArticle() {
      this.sa_loading = true
      let query = {
        form: {
          title: this.title,
          html_content: this.editorData
        }
      }
      edit_article(query,this.value1).then(res => {
        this.$message.success(res.message)
        this.editorData = ''
        this.sa_loading = false
      }).catch(err => {
        this.sa_loading = false
      })
    },
    record() {
      this.dialogVisible = true
      cache_article({type: 10,page:this.page}).then(res=> {
        console.log(res)
        this.tableData = res.data.list.data
        this.page = res.data.list.current_page
        this.per_page = res.data.list.per_page
        this.total = res.data.list.total
      })
    },
    onRowClick(row) {
      console.log(row)
      if (row.status == 20) {
        this.editorData = row.html_content
        this.title = row.title
        this.value1 = row.cache_article_id
        this.dialogVisible = false
      } else {
        this.$message.error('文章还未生成，请等待......')
      }
    },
    submitDia(row) {
      this.innerVisible = true
      this.value1 = row.cache_article_id
    },
    submit() {
      this.loading1 = true
      let query = {}
      if (this.form.type&&this.form.module) {
        query = {
          form: {
            type: this.form.type,
            module: this.form.module,
            cacheArticleId: this.value1,
            sideId:this.siteId
          }
        }
      }
      release(query).then(res=> {
        console.log(res)
        this.$message.success(res.message)
        this.editorData = ''
        this.form.type = ''
        this.form.module = ''
        this.innerVisible = false
        this.dialogVisible = false
        this.loading1 = false
      }).catch(()=> {
        this.loading1 = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.deepOriginal {
  padding: 20px 0;
  display: flex;
}
.body {
  flex: 1;
  margin-right: 40px;
}
.types {
  width: 240px;
  height: 800px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #EEF1FF;
  border-radius: 8px;
  .title {
    font-size: 16px;
  }
}
.form-btn {
  display: flex;
  justify-content: space-between;
}
.head {
  width: 100%s;
  // display: flex;
  // justify-content: space-between;
  margin-bottom: 15px;
}
.content {
  width: 100%;
  height: 700px;
  overflow-y: scroll;
  line-height: 2;
  font-size: 14px;
  .title {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    div {
      flex: 1;
    }
  }
}
.btn-content {
  display: flex;
  flex-direction: column;
}
.btn-content button {
  color: #2c2c2c;
  background-color: #D2DAFF;
  width: 160px;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  border: none;
  overflow: hidden;
  cursor: pointer;
  border-radius: 20px;
  margin-bottom: 20px;
  transition: background-color 0.3s ease-in-out, color 0.3s ease-in; /* 添加过渡效果 */
}
.btn-content button.active {
  border-radius: 20px;
  background-color: #B1B2FF;
  color: #fff;
}

.submitArticle {
  margin-top: 20px;
}
.desc {
  font-size: 14px;
  line-height: 1.5;
}
</style>

